package handlers

import (
	"GIN-API/global"
	"GIN-API/internal/models/DTO"
	"GIN-API/internal/service"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// ArticleHandler 定义文章控制器层结构体
type ArticleHandler struct {
	articleService *service.ArticleService
}

// NewArticleHandler 创建文章控制器层实例
func NewArticleHandler(articleService *service.ArticleService) *ArticleHandler {
	return &ArticleHandler{
		articleService: articleService,
	}
}

// CreateArticle 创建文章
func (ah *ArticleHandler) CreateArticle(c *gin.Context) {
	var articleDto DTO.CreateArticleRequest
	if err := c.ShouldBindJSON(&articleDto); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
		return
	}
	articleDto.AuthorID = c.GetUint("userID")
	if err := ah.articleService.CreateArticle(&articleDto); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "创建文章失败"})
		return
	}
	c.JSON(http.StatusCreated, articleDto)
}

// GetArticleByID 根据文章 ID 获取文章信息
func (ah *ArticleHandler) GetArticleByID(c *gin.Context) {
	id := c.Param("id")
	var articleID uint
	if _, err := fmt.Sscanf(id, "%d", &articleID); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的文章 ID"})
		return
	}
	article, err := ah.articleService.GetArticleByID(articleID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取文章信息失败"})
		return
	}
	if article == nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "文章不存在"})
		return
	}
	c.JSON(http.StatusOK, article)
}

// UpdateArticle 更新文章信息
func (ah *ArticleHandler) UpdateArticle(c *gin.Context) {
	id := c.Param("id")
	var articleID uint
	if _, err := fmt.Sscanf(id, "%d", &articleID); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的文章 ID"})
		return
	}
	var updatedArticle DTO.UpdatedArticleRequest
	if err := c.ShouldBindJSON(&updatedArticle); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
		global.Logger.Println("请求参数格式错误", err)
		return
	}
	updatedArticle.ID = articleID
	if err := ah.articleService.UpdateArticle(&updatedArticle); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "更新文章信息失败"})
		return
	}
	c.JSON(http.StatusOK, updatedArticle)
}

// DeleteArticle 根据文章 ID 删除文章
func (ah *ArticleHandler) DeleteArticle(c *gin.Context) {
	id := c.Param("id")
	var articleID uint
	if _, err := fmt.Sscanf(id, "%d", &articleID); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "无效的文章 ID"})
		return
	}
	if err := ah.articleService.DeleteArticle(articleID); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除文章失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "文章删除成功"})
}

// GetAllArticles 获取所有文章
func (ah *ArticleHandler) GetAllArticles(c *gin.Context) {
	var req DTO.ListArticleRequest
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数格式错误"})
		global.Logger.Println("请求参数格式错误", err)
		return
	}
	global.Logger.Println("请求参数", req)
	articles, total, err := ah.articleService.GetAllArticles(&req, c.GetUint("userID"))
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "获取文章列表失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"data":  articles,
		"total": total,
	})
}
